/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "loginpromptdialog.h"

#include <QDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QString>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>

#include <QDebug>

LoginPromptDialog :: LoginPromptDialog(QWidget* parent/*=0*/)
    :QDialog(parent)
{
    this->setWindowTitle("NUP Login prompt");

    pMainGrid = new QVBoxLayout(this);
    pLoginLayout = new QHBoxLayout();
    pPasswordLayout = new QHBoxLayout();
    pButtonLayout = new QHBoxLayout();

    pLoginLabel = new QLabel(tr("Login:"),this);
    pPasswordLabel = new QLabel(tr("Password:"),this);

    pLoginEdit = new QLineEdit(this);
    pPasswordEdit = new QLineEdit(this);
    pPasswordEdit->setEchoMode(QLineEdit::Password);

    pValidateButton = new QPushButton(tr("Ok"),this);
    pCancelButton = new QPushButton(tr("Cancel"),this);

    pMainGrid->addLayout(pLoginLayout);
        pLoginLayout->addWidget(pLoginLabel,10);
        pLoginLayout->addWidget(pLoginEdit,0,Qt::AlignRight);
    pMainGrid->addLayout(pPasswordLayout);
        pPasswordLayout->addWidget(pPasswordLabel);
        pPasswordLayout->addWidget(pPasswordEdit);
    pMainGrid->addLayout(pButtonLayout);
        pButtonLayout->addWidget(pValidateButton);
        pButtonLayout->addWidget(pCancelButton);

    connect(pValidateButton, SIGNAL(clicked()),this,SLOT(accept()));
    connect(pCancelButton, SIGNAL(clicked()),this,SLOT(reject()));

    qDebug() << "LoginPromptDialog created";
}

LoginPromptDialog :: ~LoginPromptDialog(void)
{
    qDebug() << "LoginPromptDialog deleted";
}

void LoginPromptDialog :: accept(void)
{
    qDebug() << "LoginPromptDialog :: accept";

    if ( !this->pLoginEdit->text().isEmpty() && !this->pPasswordEdit->text().isEmpty() )
    {
        this->done(QDialog::Accepted);
    }
    else
    {
        if ( this->pLoginEdit->text().isEmpty() )
        {
            QMessageBox::warning(this,tr("Error in the input"),tr("Please enter a login"));
        }
        else
        {
            QMessageBox::warning(this,tr("Error in the input"),tr("Please enter a password"));
        }
    }
}

